package jmu.cyxwcy.librarymanagement.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import jmu.cyxwcy.librarymanagement.pojo.*;
import jmu.cyxwcy.librarymanagement.service.IFinesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDate;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 
 * @since 2024-06-27
 */
@RestController
@RequestMapping("/fines")
public class FinesController {
    @Autowired
    private IFinesService iFinesService;

    //缴费
    @PutMapping("/updatePaymentStatus")
    public Result updatePaymentStatus(@RequestParam Integer fineId,@RequestParam String paymentStatus){
        iFinesService.updatePaymentStatus(fineId,paymentStatus);
        return Result.success();
    }


    @GetMapping("/page")
    public Result<IPage<Fines>> findPage(@RequestParam Integer pageNum,
                                         @RequestParam Integer pageSize,
                                         @RequestParam(defaultValue = "") Integer fineId,
                                         @RequestParam(defaultValue = "") Integer borrowingId,
                                         @RequestParam(defaultValue = "") BigDecimal totalAmount,
                                         @RequestParam(defaultValue = "") LocalDate generatedDate,
                                         @RequestParam(defaultValue = "") String bookName,
                                         @RequestParam(defaultValue = "") Integer userId) {
        IPage<Fines> page = new Page<>(pageNum, pageSize);
        MPJLambdaWrapper<Fines> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper
                .selectAll(Fines.class)
                .select(BookDescription::getBookName)
                .select(User::getUserId)
                .leftJoin(Borrowing.class,Borrowing::getBorrowingId,Fines::getBorrowingId)
                .leftJoin(Book.class,Book::getBookId,Borrowing::getBookId)
                .leftJoin(BookDescription.class,BookDescription::getIsbn,Book::getIsbn)
                .leftJoin(User.class,User::getUserId,Borrowing::getUserId)
                .like(fineId!=null, Fines::getFineId, fineId)
                .like(borrowingId!=null, Fines::getBorrowingId, borrowingId)
                .like(totalAmount!=null, Fines::getTotalAmount, totalAmount)
                .eq(generatedDate!=null, Fines::getGeneratedDate, generatedDate)
                .like(!bookName.isEmpty(), BookDescription::getBookName, bookName)
                .like(userId!=null, User::getUserId, userId);
        queryWrapper.orderByAsc("fine_id");
        IPage<Fines> pg= iFinesService.join(page, queryWrapper);
        return Result.success(pg);
    }
}
